package com.grablist.feihu.grablist.ui.actviity.user;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.ui.actviity.user.biz.SuggestionBiz;
import com.grablist.feihu.grablist.ui.actviity.user.component.DaggerSuggestionComponent;
import com.grablist.feihu.grablist.ui.actviity.user.contract.SuggestionContract;
import com.grablist.feihu.grablist.ui.actviity.user.module.SuggestionModule;
import com.grablist.feihu.grablist.ui.actviity.user.presenter.SuggestionPresenter;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import rx.functions.Action1;

/**
 * Created by admin on 2017/12/2.
 */

public class SuggestionAct extends BaseActivity implements SuggestionContract.View {


    @Inject
    SuggestionPresenter presenter;
    @Inject
    SuggestionBiz biz;
    @Bind(R.id.suggetion)
    EditText suggetion;
    @Bind(R.id.suggetion_submit)
    TextView submit;
    @Bind(R.id.suggetion_number)
    TextView suggetionNumber;
    @Bind(R.id.tv_weixin)
    TextView mTvWeixin;


    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_suggestion);
        // 设置启动Activity时不自动弹出软键盘
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    @Override
    protected void setup() {
        setStateText();
        setToolBar("意见与反馈", "");
        DaggerSuggestionComponent.builder().suggestionModule(new SuggestionModule(this)).build().inject(this);
        presenter.setBiz(biz);

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.addSuggest(suggetion.getText().toString().trim());
                    }
                });
        mTvWeixin.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText("wxid702975");
                showMsg("复制成功");
                return false;
            }
        });
        suggetion.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                suggetionNumber.setText(s.length()+"/300");
            }
        });
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();

    }
}
